//convert.cpp
#include "convert.h"

namespace COMMON_NS_CLR{

int Convert::String2Char(IN String^ strIn,char *pcOut,int %iLen)
{
	int iRet=-1,i;
	int iResult,iTemp;
	array<System::Byte> ^byteAry=nullptr;

	if(strIn==nullptr ||
		pcOut==NULL ||
		iLen<=0)
		SET_RET_AND_GOTO_EXIT(COMMON_ERR_INVALIDARGS);

	byteAry=Encoding::Default->GetBytes(strIn);
	if(byteAry.Length>=iLen) SET_RET_AND_GOTO_EXIT(COMMON_ERR_NOMEM);

	for(i=0;i<byteAry.Length;i++)
		pcOut[i]=byteAry[i];
	iLen=i;

	iRet=0;
_Exit:
	return iRet;

}

int Convert::Char2String(IN char *pcIn,int iLen,OUT String^ %strOut,Encoding^ pobjEncode)
{
	int iRet=-1,i;
	int iResult,iTemp;
	array<System::Byte> ^byteAry=nullptr;

	if(pobjEncode==nullptr ||
		pcIn==NULL ||
		iLen<=0)
		SET_RET_AND_GOTO_EXIT(COMMON_ERR_INVALIDARGS);

	byteAry=gcnew array<System::Byte>(iLen);
	for(i=0;i<iLen;i++) byteAry[i]=pcIn[i];
	strOut=pobjEncode->GetString(byteAry);

	iRet=0;
_Exit:
	return iRet;

}

}//namespace COMMON_NS_CLR
